﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators; 

namespace PSE.Framework.Service.Validation
{
    /// <summary>
    /// Classe para validações de string vazia
    /// </summary>
    internal class StringNotEmptyValidator : Validator<string>
    {
        /// <summary>
        /// Indica se deve aplicar Trim na string
        /// </summary>
        private bool _applyTrim;

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="applytrim">Indica se deve aplicar Trim da string</param>
        public StringNotEmptyValidator(bool applytrim) : base(null, null)
        {
            _applyTrim = applytrim; 
        }

        /// <summary>
        /// Método de implementação requerida (abstract na classe base) que contém a regra de validação 
        /// </summary>
        /// <param name="objectToValidate"></param>
        /// <param name="currentTarget"></param>
        /// <param name="key"></param>
        /// <param name="validationResults"></param>
        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            bool validou = false;
            if (objectToValidate != null)
            {
                if (_applyTrim)
                {
                    objectToValidate = objectToValidate.Trim(); 
                }
                if (objectToValidate != String.Empty)
                {
                    validou = true;
                }
            }
            if (!validou)
            {
                validationResults.AddResult(new ValidationResult(this.MessageTemplate, currentTarget, key, this.Tag, this));   
            }
        }

        /// <summary>
        /// Método de implementação requerida (abstract na classe base) e que retorna DefaultMessageTemplate
        /// </summary>
        protected override string DefaultMessageTemplate
        {
            get { return ""; }
        }
    }
}
